package com.ulook.lookplanet.ui.square.community;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TCommunity;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.ui.square.topic.TopicActivity;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;
import com.ulook.lookplanet.widget.CommunityView;
import com.ulook.lookplanet.widget.TopicItemView;

import java.util.List;

public class CommunityActivity extends AppCompatActivity {

    private CommunityViewModel viewModel;
    private int currUserId;
    private LinearLayout communityLinearLayout;
    private LinearLayout topicLinearLayout;
    private QMUIRadiusImageView2 commuImage;
    private TextView commuText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community);
        viewModel = new ViewModelProvider(this).get(CommunityViewModel.class);

        viewModel.getCommunityList().observe(this, communityList -> {
            displayCommunity(communityList);
        });
        viewModel.getTopicList().observe(this, topics -> {
            displayTopic(topics);
        });

        Intent intent = getIntent();
        currUserId = intent.getIntExtra("currUserId", 0);

        initTopBar();
        initData();
    }

    /**
     * 初始化TopBar
     */
    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.commu_bar);
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
        topBar.setTitle("小组话题");
    }

    /**
     * 初始化Data
     */
    private void initData(){
        communityLinearLayout = findViewById(R.id.commu_layout);
        topicLinearLayout = findViewById(R.id.commu_topic_layout);
        commuImage = findViewById(R.id.commu_head_img);
        commuText = findViewById(R.id.commu_name_text);

        commuImage.setClickable(true);
        commuImage.setOnClickListener(view -> {
            Intent intent = new Intent(CommunityActivity.this, TopicActivity.class);
            intent.putExtra("currUserId", currUserId);
            intent.putExtra("topic", viewModel.getCurrCommunity());
            startActivity(intent);
        });

        viewModel.getCommunityData();
    }

    /**
     * 展示小组列表
     */
    private void displayCommunity(List<TCommunity> communities){
        for(int i = 0; i < communities.size(); i++){
            CommunityView communityView = new CommunityView(this);
            communityView.setWidgetData(communities.get(i));
            communityView.setClickable(true);
            // 点击事件
            communityView.setOnClickListener(view -> {
                TCommunity community = ((CommunityView)view).getCommunity();
                Bitmap bitmap = ((CommunityView)view).getCommuImage();
                commuImage.setImageBitmap(bitmap);
                commuText.setText(community.getName());
                viewModel.setCurrCommunity(community);
                viewModel.getTopicsForComuId(community.getUid());
                topicLinearLayout.removeAllViews();
            });
            communityLinearLayout.addView(communityView);
            // 默认展示第一个小组的话题
            if(i == 0){
                TCommunity community = communities.get(0);
                ResponseCallback callback = new ResponseCallback() {
                    @Override
                    public void onSuccess(Object res) {
                        byte[] image = (byte[]) res;
                        Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                        commuImage.setImageBitmap(bitmap);
                    }
                    @Override
                    public void onFailure(OkHttpException failuer) {
                        ThrowException.print(failuer, failuer.getEmsg());
                    }
                };
                AsynRequestUtil.loadImg(AppServer.DEFAULT_IMG_PATH + community.getComuimg(), null, callback);
                commuText.setText(community.getName());
                viewModel.setCurrCommunity(community);
                viewModel.getTopicsForComuId(communities.get(0).getUid());
            }
        }
    }

    /**
     * 展示话题列表
     */
    private void displayTopic(List<TTopic> topics){
        for(int i = 0; i < topics.size(); i++){
            TTopic tTopic = topics.get(i);
            TopicItemView topicItemView = new TopicItemView(this);
            topicItemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            topicItemView.setWidgetData(topics.get(i), currUserId);
            topicItemView.setClickable(true);
            topicItemView.setOnClickListener(view -> {
                // 跳转话题页面
                Intent intent = new Intent(CommunityActivity.this, TopicActivity.class);
                intent.putExtra("topic", tTopic);
                intent.putExtra("currUserId", currUserId);
                startActivity(intent);
            });
            topicLinearLayout.addView(topicItemView);
        }
    }

}